package org.oreilly.javaGoodParts.examples;

import java.util.Map;
import java.util.Set;

import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Sets;

public class Leaderboard {

	private final Set<Leaderboard.Entry> board = Sets.newTreeSet();

	public Leaderboard(Map<?, Float> map) {
		for (Map.Entry<?, Float> entry : map.entrySet()) {
			board.add(new Entry(entry.getKey().toString(), entry.getValue()));
		}
	}

	@Override
	public String toString() {
		return board.toString();
	}

	private static class Entry implements Comparable<Entry> {

		private final String label;
		private final float value;

		public Entry(String label, float value) {
			this.label = label;
			this.value = value;
		}

		@Override
		public int compareTo(Leaderboard.Entry that) {
			return ComparisonChain.start()
				.compare(that.value, value)
				.compare(label, that.label).result();
		}

		@Override
		public String toString() {
			return label + ": " + value;
		}
	}
}
